# This code is to create pension dataset (imputed based on categorical variable if NA) for PSID 
# Created by Shuyi Qiu
# Generated on May. 7th, 2024
# Last edited on May. 7th, 2024

# 0. Set-up ----
set.seed(27705)
setwd("/Users/RachelChiu/Documents/ReProj/PSID/spell")
library(tidyverse)
library(ggplot2)
library(psidread)
library(dplyr)
library(haven)
library(readxl)
library(data.table)
library(Amelia)
library(janitor)
library(mitools)
library(mice)


# 1. Import data ----
psid_varlist <- c(
  ## 1.1 Head's current account ----
  "cpenh || [99]ER15181 [01]ER19349 [03]ER22744 [05]ER26725 [07]ER37761 [09]ER43734 
  [11]ER49080 [13]ER54836 [15]ER61956 [17]ER68010 [19]ER74036 [21]ER80159",
  "cpenhge10k || [07]ER37762 [09]ER43735",
  "cpenhge20k || [07]ER37763 [09]ER43736 [11]ER49084 [13]ER54840 [15]ER61960 [17]ER68014 [19]ER74040 [21]ER80163",
  "cpenhge50k || [07]ER37764 [09]ER43737 [11]ER49081 [13]ER54837 [15]ER61957 [17]ER68011 [19]ER74037 [21]ER80160",
  "cpenhge100k || [11]ER49082 [13]ER54838 [15]ER61958 [17]ER68012 [19]ER74038 [21]ER80161",
  "cpenhge150k || [07]ER37765 [09]ER43738",
  "cpenhge200k || [11]ER49083 [13]ER54839 [15]ER61959 [17]ER68013 [19]ER74039 [21]ER80162",
  
  ## 1.2 Spouse's current account ----
  "cpenw || [99]ER15327 [01]ER19492 [03]ER22888 [05]ER26869 [07]ER37993 [09]ER43966 
  [11]ER49299 [13]ER55052 [15]ER62173 [17]ER68227 [19]ER74243 [21]ER80365",
  "cpenwge10k || [07]ER37994 [09]ER43967",
  "cpenwge20k || [07]ER37995 [09]ER43968 [11]ER49303 [13]ER55056 [15]ER62177 [17]ER68231 [19]ER74247 [21]ER80369",
  "cpenwge50k || [07]ER37996 [09]ER43969 [11]ER49300 [13]ER55053 [15]ER62174 [17]ER68228 [19]ER74244 [21]ER80366",
  "cpenwge100k || [11]ER49301 [13]ER55054 [15]ER62175 [17]ER68229 [19]ER74245 [21]ER80367",
  "cpenwge150k || [07]ER37997 [09]ER43970",
  "cpenwge200k || [11]ER49302 [13]ER55055 [15]ER62176 [17]ER68230 [19]ER74246 [21]ER80368",
  
  ## 1.3 Head's previous account (491) ----
  "penp491h || [99]ER15223 [01]ER19391 [03]ER22786 [05]ER26767 [07]ER37815 [09]ER43788 
  [11]ER49122 [13]ER54876 [15]ER61997 [17]ER68051 [19]ER74073 [21]ER80195",
  "penp491hge20k || [07]ER37817 [09]ER43790 [11]ER49126 [13]ER54880 [15]ER62001 [17]ER68055 [19]ER74077 [21]ER80199",
  "penp491hge50k || [07]ER37818 [09]ER43791 [11]ER49123 [13]ER54877 [15]ER61998 [17]ER68052 [19]ER74074 [21]ER80196",
  "penp491hge100k || [11]ER49124 [13]ER54878 [15]ER61999 [17]ER68053 [19]ER74075 [21]ER80197",
  "penp491hge150k || [07]ER37819 [09]ER43792",
  "penp491hge200k || [11]ER49125 [13]ER54879 [15]ER62000 [17]ER68054 [19]ER74076 [21]ER80198",
  
  ## 1.4 Head's previous account (492) ----
  "penp492h || [99]ER15264 [01]ER19432 [03]ER22827 [05]ER26808 [07]ER37896 [09]ER43869 
  [11]ER49202 [13]ER54956 [15]ER62077 [17]ER68131 [19]ER74151 [21]ER80273",
  "penp492hge20k || [07]ER37898 [09]ER43871 [11]ER49206 [13]ER54960 [15]ER62081 [17]ER68135 [19]ER74155 [21]ER80277",
  "penp492hge50k || [07]ER37899 [09]ER43872 [11]ER49203 [13]ER54957 [15]ER62078 [17]ER68132 [19]ER74152 [21]ER80274",
  "penp492hge100k || [11]ER49204 [13]ER54958 [15]ER62079 [17]ER68133 [19]ER74153 [21]ER80275",
  "penp492hge150k || [07]ER37900 [09]ER43873",
  "penp492hge200k || [11]ER49205 [13]ER54959 [15]ER62080 [17]ER68134 [19]ER74154 [21]ER80276",
  
  ## 1.5 Spouse's previous account (491) ----
  "penp491w || [99]ER15369 [01]ER19534 [03]ER22930 [05]ER26911 [07]ER38047 [09]ER44020 
  [11]ER49341 [13]ER55092 [15]ER62214 [17]ER68268 [19]ER74280 [21]ER80401",
  "penp491wge20k || [07]ER38049 [09]ER44022 [11]ER49345 [13]ER55096 [15]ER62218 [17]ER68272 [19]ER74284 [21]ER80405",
  "penp491wge50k || [07]ER38050 [09]ER44023 [11]ER49342 [13]ER55093 [15]ER62215 [17]ER68269 [19]ER74281 [21]ER80402",
  "penp491wge100k || [11]ER49343 [13]ER55094 [15]ER62216 [17]ER68270 [19]ER74282 [21]ER80403",
  "penp491wge150k || [07]ER38051 [09]ER44024",
  "penp491wge200k || [11]ER49344 [13]ER55095 [15]ER62217 [17]ER68271 [19]ER74283 [21]ER80404",
  
  ## 1.6 Spouse's previous account (492) ----
  "penp492w || [99]ER15410 [01]ER19575 [03]ER22971 [05]ER26952 [07]ER38128 [09]ER44101 
  [11]ER49421 [13]ER55172 [15]ER62294 [17]ER68348 [19]ER74358 [21]ER80479",
  "penp492wge20k || [07]ER38130 [09]ER44103 [11]ER49425 [13]ER55176 [15]ER62298 [17]ER68352 [19]ER74362 [21]ER80483",
  "penp492wge50k || [07]ER38131 [09]ER44104 [11]ER49422 [13]ER55173 [15]ER62295 [17]ER68349 [19]ER74359 [21]ER80480",
  "penp492wge100k || [11]ER49423 [13]ER55174 [15]ER62296 [17]ER68350 [19]ER74360 [21]ER80481",
  "penp492wge150k || [07]ER38132 [09]ER44105",
  "penp492wge200k || [11]ER49424 [13]ER55175 [15]ER62297 [17]ER68351 [19]ER74361 [21]ER80482",
  
  ## 1.7 Head's previous account (651) ----
  "penp651h || [99]ER15255 [01]ER19423 [03]ER22818 [05]ER26799 [07]ER37875 [09]ER43848 
  [11]ER49181 [13]ER54936 [15]ER62057 [17]ER68111 [19]ER74131 [21]ER80253",
  "penp651hge20k || [07]ER37877 [09]ER43850 [11]ER49185 [13]ER54940 [15]ER62061 [17]ER68115 [19]ER74135 [21]ER80257",
  "penp651hge50k || [07]ER37878 [09]ER43851 [11]ER49182 [13]ER54937 [15]ER62058 [17]ER68112 [19]ER74132 [21]ER80254",
  "penp651hge100k || [11]ER49183 [13]ER54938 [15]ER62059 [17]ER68113 [19]ER74133 [21]ER80255",
  "penp651hge150k || [07]ER37879 [09]ER43852",
  "penp651hge200k || [11]ER49184 [13]ER54939 [15]ER62060 [17]ER68114 [19]ER74134 [21]ER80256",
  
  ## 1.8 Head's previous account (652) ----
  "penp652h || [99]ER15296 [01]ER19464 [03]ER22859 [05]ER26840 [07]ER37956 [09]ER43929 
  [11]ER49261 [13]ER55016 [15]ER62137 [17]ER68191 [19]ER74209 [21]ER80331",
  "penp652hge20k || [07]ER37958 [09]ER43931 [11]ER49265 [13]ER55020 [15]ER62141 [17]ER68195 [19]ER74213 [21]ER80335",
  "penp652hge50k || [07]ER37959 [09]ER43932 [11]ER49262 [13]ER55017 [15]ER62138 [17]ER68192 [19]ER74210 [21]ER80332",
  "penp652hge100k || [11]ER49263 [13]ER55018 [15]ER62139 [17]ER68193 [19]ER74211 [21]ER80333",
  "penp652hge150k || [07]ER37960 [09]ER43933",
  "penp652hge200k || [11]ER49264 [13]ER55019 [15]ER62140 [17]ER68194 [19]ER74212 [21]ER80334",
  
  ## 1.9 Spouse's previous account (651) ----
  "penp651w || [99]ER15401 [01]ER19566 [03]ER22962 [05]ER26943 [07]ER38107 [09]ER44080 
  [11]ER49400 [13]ER55152 [15]ER62274 [17]ER68328 [19]ER74338 [21]ER80459",
  "penp651wge20k || [07]ER38109 [09]ER44082 [11]ER49404 [13]ER55156 [15]ER62278 [17]ER68332 [19]ER74342 [21]ER80463",
  "penp651wge50k || [07]ER38110 [09]ER44083 [11]ER49401 [13]ER55153 [15]ER62275 [17]ER68329 [19]ER74339 [21]ER80460",
  "penp651wge100k || [11]ER49402 [13]ER55154 [15]ER62276 [17]ER68330 [19]ER74340 [21]ER80461",
  "penp651wge150k || [07]ER38111 [09]ER44084",
  "penp651wge200k || [11]ER49403 [13]ER55155 [15]ER62277 [17]ER68331 [19]ER74341 [21]ER80462",
  
  ## 1.10 Spouse's previous account (652) ----
  "penp652w || [99]ER15442 [01]ER19607 [03]ER23003 [05]ER26984 [07]ER38188 [09]ER44161 
  [11]ER49480 [13]ER55232 [15]ER62354 [17]ER68408 [19]ER74416 [21]ER80537",
  "penp652wge20k || [07]ER38190 [09]ER44163 [11]ER49484 [13]ER55236 [15]ER62358 [17]ER68412 [19]ER74420 [21]ER80541",
  "penp652wge50k || [07]ER38191 [09]ER44164 [11]ER49481 [13]ER55233 [15]ER62355 [17]ER68409 [19]ER74417 [21]ER80538",
  "penp652wge100k || [11]ER49482 [13]ER55234 [15]ER62356 [17]ER68410 [19]ER74418 [21]ER80539",
  "penp652wge150k || [07]ER38192 [09]ER44165",
  "penp652wge200k || [11]ER49483 [13]ER55235 [15]ER62357 [17]ER68411 [19]ER74419 [21]ER80540"
  
)

str_df <- psid_str(varlist = psid_varlist, type = "separated")
psid_indir <- "/Users/RachelChiu/Documents/ReProj/PSID/Data"
psid_df <- psid_read(indir = psid_indir, 
                     str_df = str_df,
                     idvars = c("ER32000","ER32013","ER32020"),
                     type = "package",
                     filename = NA)
df <- psid_reshape(psid_df = psid_df,
                   str_df = str_df,
                   shape = "long",
                   level = "household")

# 2. Reformat ----
pension_df <- df |> 
  mutate(
    ## 2.0 NA values ----
    across(.cols = c("cpenh","cpenw"),
           .fns = ~ ifelse(. > 999999996, NA, .)),
    across(.cols = c("penp491h","penp492h","penp491w","penp492w","penp651h","penp652h","penp651w","penp652w"),
           .fns = ~ ifelse(. > 99999996, NA, .)),
    across(.cols = ends_with("0k"),
           .fns = ~ ifelse(. %in% c(1,5), ., NA)),
    
    ## 2.1 Current account (head & spouse) ----
    cpenh_imp = case_when(
      year %in% c(2007, 2009) & (is.na(cpenh)|cpenh == 0) & cpenhge10k == 1 & cpenhge20k == 5 ~ 15000,
      year %in% c(2007, 2009) & (is.na(cpenh)|cpenh == 0) & cpenhge20k == 1 & cpenhge50k == 5 ~ 35000,
      year %in% c(2007, 2009) & (is.na(cpenh)|cpenh == 0) & cpenhge50k == 1 & cpenhge150k == 5 ~ 100000,
      year %in% c(2007, 2009) & (is.na(cpenh)|cpenh == 0) & cpenhge150k == 1 ~ 150000,
      year >= 2011 & (is.na(cpenh)|cpenh == 0) & cpenhge20k == 1 & cpenhge50k == 5 ~ 35000,
      year >= 2011 & (is.na(cpenh)|cpenh == 0) & cpenhge50k == 1 & cpenhge100k == 5 ~ 75000,
      year >= 2011 & (is.na(cpenh)|cpenh == 0) & cpenhge100k == 1 & cpenhge200k == 5 ~ 150000,
      year >= 2011 & (is.na(cpenh)|cpenh == 0) & cpenhge200k == 1 ~ 200000,
      !is.na(cpenh) ~ cpenh
    ),
    cpenw_imp = case_when(
      year %in% c(2007, 2009) & (is.na(cpenw)|cpenw == 0) & cpenwge10k == 1 & cpenwge20k == 5 ~ 15000,
      year %in% c(2007, 2009) & (is.na(cpenw)|cpenw == 0) & cpenwge20k == 1 & cpenwge50k == 5 ~ 35000,
      year %in% c(2007, 2009) & (is.na(cpenw)|cpenw == 0) & cpenwge50k == 1 & cpenwge150k == 5 ~ 100000,
      year %in% c(2007, 2009) & (is.na(cpenw)|cpenw == 0) & cpenwge150k == 1 ~ 150000,
      year >= 2011 & (is.na(cpenw)|cpenw == 0) & cpenwge20k == 1 & cpenwge50k == 5 ~ 35000,
      year >= 2011 & (is.na(cpenw)|cpenw == 0) & cpenwge50k == 1 & cpenwge100k == 5 ~ 75000,
      year >= 2011 & (is.na(cpenw)|cpenw == 0) & cpenwge100k == 1 & cpenwge200k == 5 ~ 150000,
      year >= 2011 & (is.na(cpenw)|cpenw == 0) & cpenwge200k == 1 ~ 200000,
      !is.na(cpenw) ~ cpenw
    ),
    
    ## 2.2 Previous account (491) ----
    penp491h_imp = case_when(
      year %in% c(2007, 2009) & (is.na(penp491h)|penp491h == 0) & penp491hge20k == 1 & penp491hge50k == 5 ~ 35000,
      year %in% c(2007, 2009) & (is.na(penp491h)|penp491h == 0) & penp491hge50k == 1 & penp491hge150k == 5 ~ 100000,
      year %in% c(2007, 2009) & (is.na(penp491h)|penp491h == 0) & penp491hge150k == 1 ~ 150000,
      year >= 2011 & (is.na(penp491h)|penp491h == 0) & penp491hge20k == 1 & penp491hge50k == 5 ~ 35000,
      year >= 2011 & (is.na(penp491h)|penp491h == 0) & penp491hge50k == 1 & penp491hge100k == 5 ~ 75000,
      year >= 2011 & (is.na(penp491h)|penp491h == 0) & penp491hge100k == 1 & penp491hge200k == 5 ~ 150000,
      year >= 2011 & (is.na(penp491h)|penp491h == 0) & penp491hge200k == 1 ~ 200000,
      !is.na(penp491h) ~ penp491h
    ),
    penp491w_imp = case_when(
      year %in% c(2007, 2009) & (is.na(penp491w)|penp491w == 0) & penp491wge20k == 1 & penp491wge50k == 5 ~ 35000,
      year %in% c(2007, 2009) & (is.na(penp491w)|penp491w == 0) & penp491wge50k == 1 & penp491wge150k == 5 ~ 100000,
      year %in% c(2007, 2009) & (is.na(penp491w)|penp491w == 0) & penp491wge150k == 1 ~ 150000,
      year >= 2011 & (is.na(penp491w)|penp491w == 0) & penp491wge20k == 1 & penp491wge50k == 5 ~ 35000,
      year >= 2011 & (is.na(penp491w)|penp491w == 0) & penp491wge50k == 1 & penp491wge100k == 5 ~ 75000,
      year >= 2011 & (is.na(penp491w)|penp491w == 0) & penp491wge100k == 1 & penp491wge200k == 5 ~ 150000,
      year >= 2011 & (is.na(penp491w)|penp491w == 0) & penp491wge200k == 1 ~ 200000,
      !is.na(penp491w) ~ penp491w
    ),
    
    ## 2.3 Previous account (492) ----
    penp492h_imp = case_when(
      year %in% c(2007, 2009) & (is.na(penp492h)|penp492h == 0) & penp492hge20k == 1 & penp492hge50k == 5 ~ 35000,
      year %in% c(2007, 2009) & (is.na(penp492h)|penp492h == 0) & penp492hge50k == 1 & penp492hge150k == 5 ~ 100000,
      year %in% c(2007, 2009) & (is.na(penp492h)|penp492h == 0) & penp492hge150k == 1 ~ 150000,
      year >= 2011 & (is.na(penp492h)|penp492h == 0) & penp492hge20k == 1 & penp492hge50k == 5 ~ 35000,
      year >= 2011 & (is.na(penp492h)|penp492h == 0) & penp492hge50k == 1 & penp492hge100k == 5 ~ 75000,
      year >= 2011 & (is.na(penp492h)|penp492h == 0) & penp492hge100k == 1 & penp492hge200k == 5 ~ 150000,
      year >= 2011 & (is.na(penp492h)|penp492h == 0) & penp492hge200k == 1 ~ 200000,
      !is.na(penp492h) ~ penp492h
    ),
    penp492w_imp = case_when(
      year %in% c(2007, 2009) & (is.na(penp492w)|penp492w == 0) & penp492wge20k == 1 & penp492wge50k == 5 ~ 35000,
      year %in% c(2007, 2009) & (is.na(penp492w)|penp492w == 0) & penp492wge50k == 1 & penp492wge150k == 5 ~ 100000,
      year %in% c(2007, 2009) & (is.na(penp492w)|penp492w == 0) & penp492wge150k == 1 ~ 150000,
      year >= 2011 & (is.na(penp492w)|penp492w == 0) & penp492wge20k == 1 & penp492wge50k == 5 ~ 35000,
      year >= 2011 & (is.na(penp492w)|penp492w == 0) & penp492wge50k == 1 & penp492wge100k == 5 ~ 75000,
      year >= 2011 & (is.na(penp492w)|penp492w == 0) & penp492wge100k == 1 & penp492wge200k == 5 ~ 150000,
      year >= 2011 & (is.na(penp492w)|penp492w == 0) & penp492wge200k == 1 ~ 200000,
      !is.na(penp492w) ~ penp492w
    ),
    
    ## 2.4 Previous account (651) ----
    penp651h_imp = case_when(
      year %in% c(2007, 2009) & (is.na(penp651h)|penp651h == 0) & penp651hge20k == 1 & penp651hge50k == 5 ~ 35000,
      year %in% c(2007, 2009) & (is.na(penp651h)|penp651h == 0) & penp651hge50k == 1 & penp651hge150k == 5 ~ 100000,
      year %in% c(2007, 2009) & (is.na(penp651h)|penp651h == 0) & penp651hge150k == 1 ~ 150000,
      year >= 2011 & (is.na(penp651h)|penp651h == 0) & penp651hge20k == 1 & penp651hge50k == 5 ~ 35000,
      year >= 2011 & (is.na(penp651h)|penp651h == 0) & penp651hge50k == 1 & penp651hge100k == 5 ~ 75000,
      year >= 2011 & (is.na(penp651h)|penp651h == 0) & penp651hge100k == 1 & penp651hge200k == 5 ~ 150000,
      year >= 2011 & (is.na(penp651h)|penp651h == 0) & penp651hge200k == 1 ~ 200000,
      !is.na(penp651h) ~ penp651h
    ),
    penp651w_imp = case_when(
      year %in% c(2007, 2009) & (is.na(penp651w)|penp651w == 0) & penp651wge20k == 1 & penp651wge50k == 5 ~ 35000,
      year %in% c(2007, 2009) & (is.na(penp651w)|penp651w == 0) & penp651wge50k == 1 & penp651wge150k == 5 ~ 100000,
      year %in% c(2007, 2009) & (is.na(penp651w)|penp651w == 0) & penp651wge150k == 1 ~ 150000,
      year >= 2011 & (is.na(penp651w)|penp651w == 0) & penp651wge20k == 1 & penp651wge50k == 5 ~ 35000,
      year >= 2011 & (is.na(penp651w)|penp651w == 0) & penp651wge50k == 1 & penp651wge100k == 5 ~ 75000,
      year >= 2011 & (is.na(penp651w)|penp651w == 0) & penp651wge100k == 1 & penp651wge200k == 5 ~ 150000,
      year >= 2011 & (is.na(penp651w)|penp651w == 0) & penp651wge200k == 1 ~ 200000,
      !is.na(penp651w) ~ penp651w
    ),
    
    ## 2.5 Previous account (652) ----
    penp652h_imp = case_when(
      year %in% c(2007, 2009) & (is.na(penp652h)|penp652h == 0) & penp652hge20k == 1 & penp652hge50k == 5 ~ 35000,
      year %in% c(2007, 2009) & (is.na(penp652h)|penp652h == 0) & penp652hge50k == 1 & penp652hge150k == 5 ~ 100000,
      year %in% c(2007, 2009) & (is.na(penp652h)|penp652h == 0) & penp652hge150k == 1 ~ 150000,
      year >= 2011 & (is.na(penp652h)|penp652h == 0) & penp652hge20k == 1 & penp652hge50k == 5 ~ 35000,
      year >= 2011 & (is.na(penp652h)|penp652h == 0) & penp652hge50k == 1 & penp652hge100k == 5 ~ 75000,
      year >= 2011 & (is.na(penp652h)|penp652h == 0) & penp652hge100k == 1 & penp652hge200k == 5 ~ 150000,
      year >= 2011 & (is.na(penp652h)|penp652h == 0) & penp652hge200k == 1 ~ 200000,
      !is.na(penp652h) ~ penp652h
    ),
    penp652w_imp = case_when(
      year %in% c(2007, 2009) & (is.na(penp652w)|penp652w == 0) & penp652wge20k == 1 & penp652wge50k == 5 ~ 35000,
      year %in% c(2007, 2009) & (is.na(penp652w)|penp652w == 0) & penp652wge50k == 1 & penp652wge150k == 5 ~ 100000,
      year %in% c(2007, 2009) & (is.na(penp652w)|penp652w == 0) & penp652wge150k == 1 ~ 150000,
      year >= 2011 & (is.na(penp652w)|penp652w == 0) & penp652wge20k == 1 & penp652wge50k == 5 ~ 35000,
      year >= 2011 & (is.na(penp652w)|penp652w == 0) & penp652wge50k == 1 & penp652wge100k == 5 ~ 75000,
      year >= 2011 & (is.na(penp652w)|penp652w == 0) & penp652wge100k == 1 & penp652wge200k == 5 ~ 150000,
      year >= 2011 & (is.na(penp652w)|penp652w == 0) & penp652wge200k == 1 ~ 200000,
      !is.na(penp652w) ~ penp652w
    ),
    
    ## 2.5 Summary ----
    wtr_cpenh_imp = ifelse(is.na(cpenh) & !is.na(cpenh_imp), TRUE, FALSE),
    wtr_cpenw_imp = ifelse(is.na(cpenw) & !is.na(cpenw_imp), TRUE, FALSE),
    wtr_penp491h_imp = ifelse(is.na(penp491h) & !is.na(penp491h_imp), TRUE, FALSE),
    wtr_penp491w_imp = ifelse(is.na(penp491w) & !is.na(penp491w_imp), TRUE, FALSE),
    wtr_penp492h_imp = ifelse(is.na(penp492h) & !is.na(penp492h_imp), TRUE, FALSE),
    wtr_penp492w_imp = ifelse(is.na(penp492w) & !is.na(penp492w_imp), TRUE, FALSE),
    wtr_penp651h_imp = ifelse(is.na(penp651h) & !is.na(penp651h_imp), TRUE, FALSE),
    wtr_penp651w_imp = ifelse(is.na(penp651w) & !is.na(penp651w_imp), TRUE, FALSE),
    wtr_penp652h_imp = ifelse(is.na(penp652h) & !is.na(penp652h_imp), TRUE, FALSE),
    wtr_penp652w_imp = ifelse(is.na(penp652w) & !is.na(penp652w_imp), TRUE, FALSE),
    wtr_imp = ifelse(wtr_cpenh_imp == TRUE | wtr_cpenw_imp == TRUE | 
                       wtr_penp491h_imp == TRUE | wtr_penp491w_imp == TRUE |
                       wtr_penp492h_imp == TRUE | wtr_penp492w_imp == TRUE |
                       wtr_penp651h_imp == TRUE | wtr_penp651w_imp == TRUE |
                       wtr_penp652h_imp == TRUE | wtr_penp652w_imp == TRUE, TRUE, FALSE),
    pension_acc = cpenh_imp + cpenw_imp + penp491h_imp + penp491w_imp + 
      penp492h_imp + penp492w_imp + penp651h_imp + penp651w_imp +
      penp652h_imp + penp652w_imp
  )

pension_imp <- pension_df |> 
  select(ends_with("_imp"), year, indfid, xsqnr, pension_acc)

save(pension_imp, file = "AddData/pension_imp.RData")




